package com.ygqh.baby.service.impl;

import java.util.Date;
import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.po.YgFunnyVideoPraise;
import com.ygqh.baby.mapper.YgFunnyVideoPraiseMapper;
import com.ygqh.baby.service.YgFunnyVideoPraiseService;

import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgFunnyVideoPraiseExample;
import com.ygqh.baby.ao.DataStatus;

@Service
public class YgFunnyVideoPraiseServiceImpl implements YgFunnyVideoPraiseService {

	@Autowired
	private YgFunnyVideoPraiseMapper ygFunnyVideoPraiseMapper;

	@Override
	public List<YgFunnyVideoPraise> find() {
		YgFunnyVideoPraiseExample example = new YgFunnyVideoPraiseExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygFunnyVideoPraiseMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgFunnyVideoPraiseExample example = new YgFunnyVideoPraiseExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygFunnyVideoPraiseMapper.countByExample(example);
	}

	@Override
	public YgFunnyVideoPraise findById(Long id) {
		return ygFunnyVideoPraiseMapper.selectByPrimaryKey(id);
	}

	@Override
	public int save(YgFunnyVideoPraise ygFunnyVideoPraise) {
		return ygFunnyVideoPraiseMapper.insertSelective(ygFunnyVideoPraise);
	}

	@Override
	public int update(YgFunnyVideoPraise ygFunnyVideoPraise) {
		return ygFunnyVideoPraiseMapper.updateByPrimaryKeySelective(ygFunnyVideoPraise);
	}

	@Override
	public ResultSet<YgFunnyVideoPraise> search(QueryInfo queryInfo, String q) {
		List<YgFunnyVideoPraise> list = ygFunnyVideoPraiseMapper.selectSuper(queryInfo, q);
		int count = ygFunnyVideoPraiseMapper.countSuper(q);
		ResultSet<YgFunnyVideoPraise> resultSet = new ResultSet<YgFunnyVideoPraise>(count, list);
		return resultSet;
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgFunnyVideoPraiseExample example = new YgFunnyVideoPraiseExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgFunnyVideoPraise record = new YgFunnyVideoPraise();
		record.setStatus(DataStatus.Delete);

		return ygFunnyVideoPraiseMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[] { id });
	}

	@Override
	public int saveOrUpdate(YgFunnyVideoPraise ygFunnyVideoPraise) {
		int r = 0;
		if (ygFunnyVideoPraise.getId() != null) {
			r = this.update(ygFunnyVideoPraise);
		} else {
			r = this.save(ygFunnyVideoPraise);
		}
		return r;
	}

	@Override
	public Boolean hasPoint(String openId, Long videoId) {
		YgFunnyVideoPraiseExample example = new YgFunnyVideoPraiseExample();
		example.createCriteria().andVideoIdEqualTo(videoId).andOpenIdEqualTo(openId)
				.andStatusEqualTo(DataStatus.Valid.name());
		int i = ygFunnyVideoPraiseMapper.countByExample(example);

		return i > 0 ? true : false;
	}

	@Override
	public int addPraise(Long videoId, String openId, String nickName, String headUrl) {
		YgFunnyVideoPraise funnyVideoPraise = new YgFunnyVideoPraise(videoId, openId, nickName, headUrl);
		funnyVideoPraise.setCreateTime(new Date());

		return this.save(funnyVideoPraise);
	}

	@Override
	public int deleteSoft(Long videoId, String userNameOrOpenId) {
		YgFunnyVideoPraiseExample example = new YgFunnyVideoPraiseExample();
		example.createCriteria().andVideoIdEqualTo(videoId).andOpenIdEqualTo(userNameOrOpenId);
		YgFunnyVideoPraise record = new YgFunnyVideoPraise();
		record.setStatus(DataStatus.Delete);
		record.setCancelTime(new Date());
		return ygFunnyVideoPraiseMapper.updateByExampleSelective(record, example);
	}

}
